package com.cyt.gen.utils;

import cn.org.rapid_framework.generator.GeneratorProperties;
import com.cyt.gen.model.vo.FormFieldVo;
import com.cyt.gen.model.vo.FormTableVo;
import com.cyt.gen.model.vo.GeneratorVo;
import com.cyt.gen.service.FormFieldService;
import com.cyt.gen.service.FormTableService;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author rong yuan
 * @date 2019/7/4 15:40
 */
public class GeneratorFormUtil extends GeneratorUtil {

    private FormTableService formTableService;
    private FormFieldService formFieldService;

    GeneratorFormUtil(GeneratorVo generatorVo, FormTableService formTableService, FormFieldService formFieldService) {
        super(generatorVo);
        this.formTableService = formTableService;
        this.formFieldService = formFieldService;
    }

    @Override
    public void generatorCode() throws Exception {
        //删除已生成或已存在代码
        deleteGeneratorCode();
        //初始化配置属性
        initGeneratorProperties();
        //生成java代码
        startGeneratorCode();
    }

    @Override
    public void initGeneratorProperties() {
        super.initGeneratorProperties();
        FormFieldVo formFieldVo = new FormFieldVo(tableName);
        GeneratorProperties.setProperty("relateInfo", formFieldService.selectRelateInfo(formFieldVo));
        formFieldVo.setClassify(String.valueOf(1));
        GeneratorProperties.setProperty("searchNotRelate", formFieldService.selectNotRelateInfo(formFieldVo));
        formFieldVo.setClassify(String.valueOf(2));
        GeneratorProperties.setProperty("showIsRelate", formFieldService.selectRelateInfo(formFieldVo));
        formFieldVo.setClassify(String.valueOf(3));
        GeneratorProperties.setProperty("fieldIsRelate", formFieldService.selectRelateInfo(formFieldVo));
    }

    Map<String, Object> getDataModel() {
        Map<String, Object> dataModel = new HashMap<>();
        dataModel.put("tableComment", moduleLogName + "-" + classLogName);
        dataModel.put("moduleLogName", moduleLogName);
        dataModel.put("classLogName", classLogName);
        dataModel.put("module", moduleName);
        dataModel.put("entity", entityName);
        dataModel.put("author", authorName);
        dataModel.put("tableName", tableName);
        FormTableVo formTableVo = new FormTableVo(tableName);
        formTableVo = formTableService.selectForPreviewList(formTableVo);
        dataModel.put("basePath", "${s.base}");
        dataModel.put("formTool", formTableVo.getFormTool());
        dataModel.put("formSearchVoList", formTableVo.getFormSearchVoList());
        dataModel.put("formShowVoList", formTableVo.getFormShowVoList());
        dataModel.put("searchSelectList", formTableVo.getSearchSelectList());
        dataModel.put("showSelectList", formTableVo.getShowSelectList());
        dataModel.put("allSelectList", StringUtils.join(formTableVo.getAllSelectList(), ","));
        formTableVo = formTableService.selectForPreviewEdit(formTableVo);
        dataModel.put("formFieldVoList", formTableVo.getFormFieldVoList());
        dataModel.put("fieldSelectList", formTableVo.getFieldSelectList());
        dataModel.put("allSelect", StringUtils.join(formTableVo.getAllSelectList(), ","));
        FormFieldVo formFieldVo = new FormFieldVo(tableName);
        dataModel.put("relateInfo", formFieldService.selectRelateInfo(formFieldVo));
        formFieldVo.setClassify(String.valueOf(1));
        dataModel.put("searchNotRelate", formFieldService.selectNotRelateInfo(formFieldVo));
        formFieldVo.setClassify(String.valueOf(2));
        dataModel.put("showIsRelate", formFieldService.selectRelateInfo(formFieldVo));
        formFieldVo.setClassify(String.valueOf(3));
        dataModel.put("fieldIsRelate", formFieldService.selectRelateInfo(formFieldVo));
        return dataModel;
    }
}